package cn.tool.utils;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.ScriptRunner;

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class TransferFujinDataUtils {


    public static void replaceStringForFile(String filePath, String origin, String target, String targetFilePath) throws Exception {
        InputStreamReader inputStreamReader;
        OutputStreamWriter outputStreamWriter;
        if (ObjectUtils.isNotEmpty(filePath) && ObjectUtils.isNotEmpty(targetFilePath)) {
            inputStreamReader = new InputStreamReader(new FileInputStream(filePath), "UTF-8");
            outputStreamWriter = new OutputStreamWriter(new FileOutputStream(targetFilePath), "UTF-8");
        } else {
            throw new RuntimeException("文件路径不正确");
        }
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        StringBuffer stringBuffer = new StringBuffer();
        String read = null;
        while ((read = bufferedReader.readLine()) != null) {
            stringBuffer = stringBuffer.append(read);
        }
        bufferedReader.close();
        inputStreamReader.close();
        String text = stringBuffer.toString();
        String result = StringUtils.replace(text, origin, target);
        BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
        String[] split = result.split(";");
        for (String s : split) {
            bufferedWriter.write(s + ";");
            bufferedWriter.newLine();
        }
        bufferedWriter.close();
        outputStreamWriter.close();
    }


    public static String replaceStringForFile(String filePath, String origin, String target) throws Exception {
        InputStreamReader inputStreamReader;
        OutputStreamWriter outputStreamWriter;
        if (ObjectUtils.isNotEmpty(filePath)) {
            inputStreamReader = new InputStreamReader(new FileInputStream(filePath), "UTF-8");
        } else {
            throw new RuntimeException("文件路径不正确");
        }
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        StringBuffer stringBuffer = new StringBuffer();
        String read = null;
        while ((read = bufferedReader.readLine()) != null) {
            stringBuffer = stringBuffer.append(read);
        }
        bufferedReader.close();
        inputStreamReader.close();
        String text = stringBuffer.toString();
        String result = StringUtils.replace(text, origin, target);
        return result;
    }

    public static void outputToFile(String target, String targetFilePath) throws Exception {
        OutputStreamWriter outputStreamWriter;
        if (ObjectUtils.isNotEmpty(targetFilePath)) {
            outputStreamWriter = new OutputStreamWriter(new FileOutputStream(targetFilePath), "UTF-8");
        } else {
            throw new RuntimeException("文件路径不正确");
        }
        BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
        String[] split = target.split(";");
        for (String s : split) {
            bufferedWriter.write(s + ";");
            bufferedWriter.newLine();
        }
        bufferedWriter.close();
        outputStreamWriter.close();
    }

    //执行sql 脚本
    public static void executeForDatabase(String database, String filePath) {
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(database, "root", "root");
            ScriptRunner scriptRunner = new ScriptRunner(connection);
            scriptRunner.runScript(new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF-8")));
        } catch (Exception throwables) {
            throwables.printStackTrace();
        } finally {
            if (null != connection) {
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        System.out.println("脚本执行完毕" + filePath);
    }


}
